package com.demo.realtime.source;

import com.demo.realtime.common.Constans;
import org.apache.flink.connector.jdbc.JdbcInputFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 单例jdbc连接器
 *
 * @Author zengqingdong
 * @ClassName JbdcConnSingleton
 * @Date 2020/8/7 22:32
 * @Version v1.0
 **/
public class JbdcConnSingleton {

    private static final Logger logger = LoggerFactory.getLogger(JbdcConnSingleton.class);

    private static JdbcInputFormat.JdbcInputFormatBuilder jdbcInputFormatBuilder;

    private JbdcConnSingleton() {
        jdbcInputFormatBuilder = JdbcInputFormat.buildJdbcInputFormat()
                .setDrivername(Constans.DB_DRIVERNAME)
                .setDBUrl(Constans.DB_URL)
                .setUsername(Constans.DB_USERNAME)
                .setPassword(Constans.DB_PASSWORD)
                .setFetchSize(20000);
    }

    private static class JdbcConnHolder {
        private static final JbdcConnSingleton jdbcConnSigleton = new JbdcConnSingleton();
    }

    public static JdbcInputFormat.JdbcInputFormatBuilder getInstance() {
        return JdbcConnHolder.jdbcConnSigleton.getJdbcInputFormatBuilder();
    }

    private JdbcInputFormat.JdbcInputFormatBuilder getJdbcInputFormatBuilder() {
        logger.info("生成jdbc连接对象：" + jdbcInputFormatBuilder);
        return jdbcInputFormatBuilder;
    }
}
